var time = 5000;

// 服务器地址
var baseServerAdrr = "http://localhost:8080/mobileMgn/mobile";

// 测试工具 
var Debug = {
	
	alert : function(msg) {
		alert(msg);
	},
	
	log : function(msg) {
		console.log(msg);
	},
	
	dir : function(obj) {
		console.dir(obj);
	},
	
	dirxml : function(obj) {
		console.dirxml(obj);
	},
	
	debug : function(msg) {
		console.debug(msg);
	},
	
	info : function(msg) {
		console.info(msg);
	},
	
	error : function(msg) {
		console.error(msg);
	},
	
	warn : function(msg) {
		console.warn(msg);
	},
	
	out : function(obj) {
		for(var i = 0; i < obj.length; i++) {
		var a =  "<"+obj[i].tagName.toLowerCase();
			if(obj[i].id) {
				a += " id = '" + obj[i].id + "' ";
			}
			if(obj[i].style) {
				a += "style = '" + obj[i].style.cssText + "' ";
			}
			if(obj[i].className) {
				a += "class = '" +obj[i].className + "' "
			}
			a += ">" + obj[i].innerHTML + "<" + obj[i].tagName.toLowerCase() + ">"
			console.log(a);
		}
	}
}

var panUtil={
	/*ajax提交的延时等待效果 */
    ajaxLoding:{
	    start: function(loadingtitle,imgstyle) {
	    	
	       if(loadingtitle==""||loadingtitle==null||loadingtitle==undefined) {
	      	  loadingtitle="正在加载...";
	       }
	       
	       if(imgstyle==""||imgstyle==null||imgstyle==undefined) {
	       	  imgstyle="style1";
	       }
	       
	       // 屏幕宽与高
	       var screenWidth= plus.screen.resolutionWidth;
	       var screenHeight= plus.screen.resolutionHeight;
	       //判断是否显示蒙层
	       var shade=document.createElement('div');
	       shade.setAttribute('id','shade_over');
	       shade.setAttribute('style', 'position: fixed;top: 0;left: 0;right: 0;bottom: 0; background-color: #E8E4E7;opacity:0.5;z-index: 998;');
	       document.body.appendChild(shade);
	       // 创建加速方块
		   var loading =document.createElement('div');
		   loading.setAttribute('id','loading_layout');
		   var viewUrl=plus.webview.currentWebview();
		   var imgurl="";
		   if(imgstyle=="style1") {
		   	   loading.setAttribute('style','position: fixed;top: 45%;left: '+(screenWidth-200)/2+'px;width:200px;height: 20%; z-index: 1001;text-align:center;');
		   	   //Debug.log(viewUrl.getURL().split('www'));
               imgurl=viewUrl.getURL().split('www')[0]+"www/img/sysicon/loading.gif";		   
		       loading.innerHTML='<img src="'+imgurl+'" /><br/>'+loadingtitle;	
		   } else if(imgstyle=="style2") {
		   	   loading.setAttribute('style','position: fixed;top: 45%;left: '+(screenWidth-200)/2+'px;width:200px;height: 20%; z-index: 1001;text-align:center;');
		   	   imgurl=viewUrl.getURL().split('www')[0]+"www/img/sysicon/loadingU.gif";		   
		       loading.innerHTML='<img src="'+imgurl+'" style=\"border-radius:50px\" /><br/>'+loadingtitle;	
		   } 	   
		   document.body.appendChild(loading);		   
	    },
	    
	    stop: function() {
		   var shade_over = document.getElementById('shade_over');
		   var loading_layout = document.getElementById('loading_layout');
		   shade_over.parentNode.removeChild( shade_over );
		   loading_layout.parentNode.removeChild( loading_layout );	   
	    }    
   },
   
   //日期工具
   DateTool:{
   	    today:function(){
   	    	var myDate   =new Date();
            var fullyear =myDate.getFullYear();     //获取完整的年份(4位,1970-????)
            var month    =myDate.getMonth()+1;      //获取当前月份(0-11,0代表1月)
            var thisdate =myDate.getDate();      //获取当前日(1-31)
   	    	return fullyear+'/'+month+'/'+thisdate;
   	    },
   	    todayBeforeYear:function() {
   	    	var myDate   =new Date();
            var fullyear =myDate.getFullYear(); 
   	    	return fullyear+'/1/1';
   	    }
   },
   //object转String
   Object2Str:function(obj) {
   	  var ret='';
      for(var i in obj){
         ret+=i+':'+obj[i];
      }
      return ret;
   },
   ReLogin:function(){
   	  
		plus.storage.setItem("autoLogin",'false');
		//获取所有的webview
		var wvs=plus.webview.all();
        //plus.webview.close(wvs[0]);
		plus.webview.open(wvs[0].getURL());
		
		var wvs2=plus.webview.all();
		for(var i=0;i<wvs2.length-1;i++){
         	plus.webview.close(wvs[i]);
        }
   },
   /*
    * @firsttime 第一次请求时间
    * @time      请求时间间隔
    * @message   显示提示信息
    * @callback  要执行的回调函数
    */
   setCloseTime:function(firsttime,time,message,callback) {
   	  if (first==null || (new Date().getTime() - first > time)) {
    	 first = new Date().getTime();
    	 callback();
	  }else{
		 mui.toast(message);
	  }
   },
   /**
    * 显示蒙层
    */
   showShade:{
   	  show:function(){
   	    var shade=document.createElement('div');
	    shade.setAttribute('id','shade_over');
	    shade.setAttribute('style', 'position: fixed;top: 0;left: 0;right: 0;bottom: 0; background-color: #E8E4E7;opacity:0.5;z-index: 998;');
	    document.body.appendChild(shade);
   	  },
   	  hide:function(){
   	  	var shade_over = document.getElementById('shade_over');
		if(shade_over!=null){
			shade_over.parentNode.removeChild(shade_over);
		}
   	  }
   },
   /**
    *  @description 此方法依赖jquery  依赖comman.css的up上划系列css
    */
   upPan:{
   	  /**
   	   * @param {Object} titleArray      标题数组
   	   * @param {Object} dataArrays      数据数组
   	   * @param {Object} querycallback   查询回调函数
   	   * @param {Object} okcallback      确定回调函数
   	   * @param {Object} cancelcallback  取消回调函数
   	   */
   	  show:function(titleArray,dataArrays,querycallback,okcallback,cancelcallback){
   	  	var uppan = document.getElementById('up-pan');
   	  	var uppanbody;
   	  	if(uppan==null){
   	  		uppan=document.createElement('div');
   	  		uppan.setAttribute('id','up-pan');
   	  		uppan.setAttribute('class','up-pan');   	  		
            document.body.appendChild(uppan);
            
            var uppandialog=document.createElement('div');
            uppandialog.setAttribute('class','up-pan-dialog');
            uppan.appendChild(uppandialog);
            
            var uppancontent=document.createElement('div');
            uppancontent.setAttribute('class','up-pan-content');
            uppandialog.appendChild(uppancontent);
            
            var uppanheader=document.createElement('div');
            uppanheader.setAttribute('class','up-pan-header');
            uppancontent.appendChild(uppanheader);
            
            uppanheader.innerHTML="<input type=\"search\" id=\"up-search-input\" class=\"mui-input-clear up-pan-query\" placeholder=\"\">"+
               "<button type=\"button\" id=\"up-search\" class=\"mui-btn mui-btn-primary mui-btn-outlined pan-float-right\">搜索</button>";
             
            var uppanfooter=document.createElement('div');
            uppanfooter.setAttribute('class','up-pan-footer');
            uppancontent.appendChild(uppanfooter);
            
            uppanfooter.innerHTML="<button type=\"button\" id=\"up-cancle\" class=\"mui-btn mui-btn-outlined\">取消</button>"+
                "<button type=\"button\" id=\"up-ok\" class=\"mui-btn mui-btn-primary pan-float-right\">确定</button>";
                 
	        uppanbody=document.createElement('div');
            uppanbody.setAttribute('class','up-pan-body');
            uppanbody.setAttribute('id','up-pan-body');
            
            var bodyheight=$(".up-pan").height()-$(".up-pan-header").height()-$(".up-pan-footer").height()-22;
            uppanbody.setAttribute('style','height: '+bodyheight+'px;');
            uppancontent.appendChild(uppanbody);
            document.getElementById("up-search").addEventListener('tap', function() {
		       if(querycallback!=null&&querycallback!=""){
		       	  querycallback();
		       }
	        });      
            document.getElementById("up-ok").addEventListener('tap', function() {
            	
               var selectRow=$(".backcolor")[0];
   	  	       if(selectRow==null||undefined==selectRow){
   	  		     mui.toast("请先选择记录！");
   	  		     return;
   	  	       }
		       if(okcallback!=null&&okcallback!=""){
		       	  okcallback();
		       }
		       panUtil.upPan.hide();
	        });
	        document.getElementById("up-cancle").addEventListener('tap', function() {
		       if(cancelcallback!=null&&cancelcallback!=""){
		       	  cancelcallback();
		       }
		       panUtil.upPan.hide();
	        });
   	  	}else{
   	  		uppanbody=document.getElementById('up-pan-body');
   	  	}
   	  	uppanbody.innerHTML="";
   	  	var uppantable=document.createElement('table');
        uppantable.setAttribute('class','pan_table');
        uppantable.setAttribute('id','up_pan_table');
        uppanbody.appendChild(uppantable);
            
        var uppantabletr=document.createElement('tr');
        uppantable.appendChild(uppantabletr);
        if(titleArray!=null&&titleArray!=""){
        	for(var i=0;i<titleArray.length;i++){
              var uppantableth=document.createElement('th');
              uppantableth.innerText=titleArray[i];
              uppantabletr.appendChild(uppantableth);
           }
        }
        
        for(var j=0;j<dataArrays.length;j++){
        	var dataArray=dataArrays[j];
            uppantabletr=document.createElement('tr');
            uppantabletr.setAttribute('class','pan_table_tr');
            for(var i=0;i<dataArray.length;i++){
                var uppantabletd=document.createElement('td');
                if(i!=0){
                	uppantabletd.setAttribute('style',' text-align:left;');
                }
                uppantabletd.innerText=dataArray[i];
                uppantabletr.appendChild(uppantabletd);
            }
            uppantable.appendChild(uppantabletr);
        } 
	    $(".pan_table_tr").bind("click",function(){
            $(this).addClass("backcolor").siblings("tr").removeClass("backcolor");
		    $(this).siblings("tr").find("td").removeClass("backcolor_font");
			$(this).find("td").addClass("backcolor_font");
			
        });
        $("#up-pan").animate( {top :"60%"}, time);
   	  },
   	  hide:function(){
   	  	$("#up-pan").animate( {top :"100%"}, time);
   	  	var uppan = document.getElementById('up-pan');
   	  	if(uppan!=null){
   	  		uppan.parentNode.removeChild(uppan);
   	  	}
   	  },
   	  getQuerytext:function(){
   	  	return $("#up-search-input").val();
   	  },
   	  getSelectRow:function(){
   	  	var returnval;
   	  	var selectRow=$(".backcolor")[0];
   	  	if(selectRow==null||undefined==selectRow){
   	  		mui.toast("请先选择记录！");
   	  		return;
   	  	}
   	  	var tds=selectRow.childNodes;
   	  	for (var i=0;i<tds.length;i++) {
   	  		if(i==0){
   	  			returnval=tds[i].innerHTML;
   	  		}else{
   	  			returnval+="-"+tds[i].innerHTML;
   	  		} 	  		
   	  	}
   	  	return returnval;
   	  }
   },
     /**
    * 金额格式化，即使将金额转换为包含逗号的
    * @param {Object} s 传入的float数字
    * @param {Object} n 希望返回小数点几位
    */
   fmoney:function(s, n){//s:传入的float数字 ，n:希望返回小数点几位
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	var l = s.split(".")[0].split("").reverse(),
	r = s.split(".")[1];
	t = "";
	for(i = 0; i < l.length; i ++ )
	{
		t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	}
		return (t.split("").reverse().join("") + "." + r).replace("-,","-");
	},
   /**
    * 将包含逗号的金额转换为不含逗号
    * @param {Object} s 需要去掉逗号的字符串
    */
   rmoney:function(s){
   		//如果字符串后边有元,先把元去除掉
   		s = s.replace("元","").replace("万元","");
   		return parseFloat(s.replace(/[^\d\.-]/g, "")); 
    },
    /**
     * 将元转换为万元
     * @param {Object} s
     */
    ytowy:function(s){
     	return s/10000;	    
    }
   
}
 function FixTable(TableID, FixColumnNumber, width, height) {
    /// <summary>
    ///     锁定表头和列
    ///     <para> sorex.cnblogs.com </para>
    /// </summary>
    /// <param name="TableID" type="String">
    ///     要锁定的Table的ID
    /// </param>
    /// <param name="FixColumnNumber" type="Number">
    ///     要锁定列的个数
    /// </param>
    /// <param name="width" type="Number">
    ///     显示的宽度
    /// </param>
    /// <param name="height" type="Number">
    ///     显示的高度
    /// </param>
    if ($("#" + TableID + "_tableLayout").length != 0) {
        $("#" + TableID + "_tableLayout").before($("#" + TableID));
        $("#" + TableID + "_tableLayout").empty();
    }
    else {
        $("#" + TableID).after("<div id='" + TableID + "_tableLayout' style='overflow:hidden;height:" + height + "px; width:" + width + "px;'></div>");
    }
    $('<div id="' + TableID + '_tableFix"></div>'
    + '<div id="' + TableID + '_tableHead"></div>'
    + '<div id="' + TableID + '_tableColumn"></div>'
    + '<div id="' + TableID + '_tableData"></div>').appendTo("#" + TableID + "_tableLayout");
    var oldtable = $("#" + TableID);
    var tableFixClone = oldtable.clone(true);
    tableFixClone.attr("id", TableID + "_tableFixClone");
    $("#" + TableID + "_tableFix").append(tableFixClone);
    var tableHeadClone = oldtable.clone(true);
    tableHeadClone.attr("id", TableID + "_tableHeadClone");
    $("#" + TableID + "_tableHead").append(tableHeadClone);
    var tableColumnClone = oldtable.clone(true);
    tableColumnClone.attr("id", TableID + "_tableColumnClone");
    $("#" + TableID + "_tableColumn").append(tableColumnClone);
    $("#" + TableID + "_tableData").append(oldtable);
    $("#" + TableID + "_tableLayout table").each(function () {
        $(this).css("margin", "0");
    });
    var HeadHeight = $("#" + TableID + "_tableHead thead").height();
    HeadHeight += 2;
    $("#" + TableID + "_tableHead").css("height", HeadHeight);
    $("#" + TableID + "_tableFix").css("height", HeadHeight);
    var ColumnsWidth = 0;
    var ColumnsNumber = 0;
    $("#" + TableID + "_tableColumn tr:last td:lt(" + FixColumnNumber + ")").each(function () {
        ColumnsWidth += $(this).outerWidth(true);
        ColumnsNumber++;
    });
    ColumnsWidth += 2;
    if ($.browser.msie) {
        switch ($.browser.version) {
            case "7.0":
                if (ColumnsNumber >= 3) ColumnsWidth--;
                break;
            case "8.0":
                if (ColumnsNumber >= 2) ColumnsWidth--;
                break;
        }
    }
    $("#" + TableID + "_tableColumn").css("width", ColumnsWidth);
    $("#" + TableID + "_tableFix").css("width", ColumnsWidth);
    $("#" + TableID + "_tableData").scroll(function () {
        $("#" + TableID + "_tableHead").scrollLeft($("#" + TableID + "_tableData").scrollLeft());
        $("#" + TableID + "_tableColumn").scrollTop($("#" + TableID + "_tableData").scrollTop());
    });
    $("#" + TableID + "_tableFix").css({ "overflow": "hidden", "position": "relative", "z-index": "50", "background-color": "Silver" });
    $("#" + TableID + "_tableHead").css({ "overflow": "hidden", "width": width - 17, "position": "relative", "z-index": "45", "background-color": "Silver" });
    $("#" + TableID + "_tableColumn").css({ "overflow": "hidden", "height": height - 17, "position": "relative", "z-index": "40", "background-color": "Silver" });
    $("#" + TableID + "_tableData").css({ "overflow": "scroll", "width": width, "height": height, "position": "relative", "z-index": "35" });
    if ($("#" + TableID + "_tableHead").width() > $("#" + TableID + "_tableFix table").width()) {
        $("#" + TableID + "_tableHead").css("width", $("#" + TableID + "_tableFix table").width());
        $("#" + TableID + "_tableData").css("width", $("#" + TableID + "_tableFix table").width() + 17);
    }
    if ($("#" + TableID + "_tableColumn").height() > $("#" + TableID + "_tableColumn table").height()) {
        $("#" + TableID + "_tableColumn").css("height", $("#" + TableID + "_tableColumn table").height());
        $("#" + TableID + "_tableData").css("height", $("#" + TableID + "_tableColumn table").height() + 17);
    }
    $("#" + TableID + "_tableFix").offset($("#" + TableID + "_tableLayout").offset());
    $("#" + TableID + "_tableHead").offset($("#" + TableID + "_tableLayout").offset());
    $("#" + TableID + "_tableColumn").offset($("#" + TableID + "_tableLayout").offset());
    $("#" + TableID + "_tableData").offset($("#" + TableID + "_tableLayout").offset());
}
